package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.common.Result;
import com.example.demo.entity.EoExecuteDept;
import com.example.demo.mapper.ExecuteDeptMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/e-dept")
public class ExecuteDeptController extends BaseController{

    @Resource
    ExecuteDeptMapper executeDeptMapper;

    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id){
        return Result.success(executeDeptMapper.deleteById(id));
    }

    @PostMapping("/update")
    public Result<?> update(@RequestBody EoExecuteDept dept){
        return Result.success(executeDeptMapper.updateById(dept));
    }

    @GetMapping("/list/{id}")
    public Result<?> list(@PathVariable Long id){
        List<EoExecuteDept> depts = executeDeptMapper.selectAllById(id);
        return Result.success(depts);
    }

    @GetMapping("/select")
    public Result<?> select(@RequestParam String departmentId, @RequestParam Integer eoId){
        LambdaQueryWrapper<EoExecuteDept> wrapper = Wrappers.lambdaQuery();
        if(StrUtil.isNotBlank(departmentId)){
            wrapper.eq(EoExecuteDept::getDeptId, departmentId);
        }
        if(eoId != null){
            wrapper.eq(EoExecuteDept::getEoId, eoId);
        }
        return Result.success(executeDeptMapper.selectOne(wrapper));
    }
}
